﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Report;
using System.Data;
using Microsoft.Reporting.WebForms;
using SIPO.Web.Support;

namespace SIPO.Web.lists.Report
{
    public partial class rptSanctionlist : BasePage
    {
        protected override void OnLoad(EventArgs e)
        {
            recSanctionRepo recSanctionDal = new recSanctionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
            //instansiasi object controller yang dibutuhkan recSanction
            recDebtorRepo recDebtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
            recTransactionTypeRepo recTransactionTypeDal = new recTransactionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            //instansiasi list object model
            List<recDebtor> debtors = recDebtorDal.GetList();
            List<recTransactionType> transTypes = recTransactionTypeDal.GetList();
            string parameters = string.Empty;

            if (!IsPostBack)
            {
                //mengisi dropdownlist recDebtor
                ddlDebitur.Items.Clear();
                if (debtors.Count > 0)
                {
                    debtors.ForEach(delegate(recDebtor debtor)
                    {
                        ddlDebitur.Items.Add(new ListItem(debtor.DebtorCode + " - " + debtor.DebtorName, debtor.DebtorID.ToString()));
                    });
                }
                ddlDebitur.Items.Insert(0, new ListItem("-- Pilih Perusahaan --", string.Empty));

                //mengisi data dropdownlist recTransactionType
                ddlTipeSanksi.Items.Clear();
                if (transTypes.Count > 0)
                {
                    transTypes.ForEach(delegate(recTransactionType transType)
                    {
                        ddlTipeSanksi.Items.Add(new ListItem(transType.TransTypeCode + " - " + transType.TransTypeName, transType.TransTypeID.ToString()));
                    });
                }
                ddlTipeSanksi.Items.Insert(0, new ListItem("-- pilih jenis sanksi --", string.Empty));

                if (Context.Request.Params["periodeAwal"] != null || Context.Request.Params["periodeAkhir"] != null || Context.Request.Params["debtorID"] != null)
                {
                    DateTime? dPeriodeAwal = Context.Request.Params["periodeAwal"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAwal"], Helper.Culture.Indonesian);
                    DateTime? dPeriodeAkhir = Context.Request.Params["periodeAkhir"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAkhir"], Helper.Culture.Indonesian);
                    string debtorID = Context.Request.Params["debtorID"] == null ? string.Empty : Context.Request["debtorID"];
                    string sanctionTypeID = Context.Request.Params["sanctionTypeID"] == null ? string.Empty : Context.Request.Params["sanctionTypeID"];
                    string isReview = Context.Request.Params["isReview"] == null ? string.Empty : Context.Request.Params["IsReview"];
                    string isApprove = Context.Request["isApprove"] == null ? string.Empty : Context.Request["isApprove"];

                    //konversi tanggal
                    string sPeriodeAwal = string.Format("{0:yyyyMMdd}", dPeriodeAwal);
                    string sPeriodeAkhir = string.Format("{0:yyyyMMdd}", dPeriodeAkhir);

                    //filter isReview
                    if (isReview.Equals("1"))
                        parameters += "WHERE recSanction.IsReview = 'false'";
                    else if (isReview.Equals("2"))
                        parameters += "WHERE recSanction.IsReview = 'true'";

                    //filter isApprove
                    if (isReview.Equals("3"))
                    {
                        if (isApprove.Equals("1"))
                            parameters += "WHERE recSanction.IsApprove = 'false'";
                        else if (isApprove.Equals("2"))
                            parameters += "WHERE recSanction.IsApprove = 'true'";
                    }
                    else
                    {
                        if (isApprove.Equals("1"))
                            parameters += " AND recSanction.IsApprove = 'false'";
                        else if (isApprove.Equals("2"))
                            parameters += " AND recSanction.IsApprove = 'true'";
                    }

                    //filter letterDate
                    if (isReview.Equals("3") && isApprove.Equals("3"))
                    {
                        if (!sPeriodeAwal.Equals(string.Empty) && !sPeriodeAkhir.Equals(string.Empty))
                            parameters += "WHERE recSanction.LetterDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recSanction.LetterDate) AND ISNULL ('" + sPeriodeAkhir + "', recSanction.LetterDate)";
                    }
                    else
                    {
                        if (!sPeriodeAwal.Equals(string.Empty) && !sPeriodeAkhir.Equals(string.Empty))
                            parameters += " AND recSanction.LetterDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recSanction.LetterDate) AND ISNULL ('" + sPeriodeAkhir + "', recSanction.LetterDate)";
                    }

                    //filter debtorID
                    if (isReview.Equals("3") && isApprove.Equals("3") && sPeriodeAwal.Equals(string.Empty) && sPeriodeAkhir.Equals(string.Empty))
                    {
                        if (!debtorID.Equals("null"))
                            parameters += "WHERE recDebtorSector.DebtorID = ISNULL ('" + debtorID + "', recDebtorSector.DebtorID)";
                    }
                    else
                    {
                        parameters += " AND recDebtorSector.DebtorID = ISNULL ('" + debtorID + "', recDebtorSector.DebtorID)";
                    }

                    //filter sanctionTypeID
                    if (isReview.Equals("3") && isApprove.Equals("3") && sPeriodeAwal.Equals(string.Empty) && sPeriodeAkhir.Equals(string.Empty) && debtorID.Equals("null"))
                    {
                        if (!sanctionTypeID.Equals("null"))
                            parameters += "WHERE recSanction.SanctionTypeID = ISNULL ('" + sanctionTypeID + "', recSanction.SanctionTypeID)";
                    }
                    else if (!sanctionTypeID.Equals(string.Empty))
                    {
                        parameters += " AND recSanction.SanctionTypeID = ISNULL ('" + sanctionTypeID + "', recSanction.SanctionTypeID)";
                    }

                    try
                    {
                        DataTable dt = recSanctionDal.GetList(parameters);

                        rsViewer.Reset();
                        rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/Transaction/rptSanction.rdlc");
                        ReportDataSource rds = new ReportDataSource();
                        rds.Name = "DataSet1";
                        rds.Value = dt;

                        rsViewer.LocalReport.DataSources.Clear();
                        rsViewer.LocalReport.DataSources.Add(rds);
                        rsViewer.LocalReport.Refresh();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }
            }
        }
    }
}